package cn.botter.subject.application.controller;

import cn.botter.subject.application.convert.SubjectLabelDTOConverter;
import cn.botter.subject.application.dto.SubjectLabelDTO;
import cn.botter.subject.common.entity.Result;
import cn.botter.subject.domain.entity.SubjectLabelBO;
import cn.botter.subject.domain.service.SubjectDomainLabelService;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {

    @Resource
    private SubjectDomainLabelService subjectDomainLabelService;

    /**
     * 添加分类的接口
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.add.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"标签名称不能为空");
            SubjectLabelBO subjectLabelBO  = SubjectLabelDTOConverter.INSTANCE.convertDtoToBo(subjectLabelDTO);
            Boolean result = subjectDomainLabelService.add(subjectLabelBO);
            return Result.OK(result);
        }catch (Exception e){
            log.info("SubjectLabelController.add.error:{}",e.getMessage(),e);
            return Result.FAILED(e.getMessage());
        }

    }

    /**
     * 更新/修改标签功能
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.update.dto:{}", JSON.toJSONString(subjectLabelDTO));

            }
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"标签名称不能为空");
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"LabelID不能为空");
            SubjectLabelBO subjectLabelBO  = SubjectLabelDTOConverter.INSTANCE.convertDtoToBo(subjectLabelDTO);
            Boolean result = subjectDomainLabelService.update(subjectLabelBO);
            return Result.OK(result);
        }catch (Exception e){
            log.info("SubjectLabelController.update.error:{}",e.getMessage(),e);
            return Result.FAILED(e.getMessage());
        }

    }

    /**
     * 删除标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.delete.dto:{}", JSON.toJSONString(subjectLabelDTO));

            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"LabelID不能为空");
            SubjectLabelBO subjectLabelBO  = SubjectLabelDTOConverter.INSTANCE.convertDtoToBo(subjectLabelDTO);
            Boolean result = subjectDomainLabelService.delete(subjectLabelBO);
            return Result.OK(result);
        }catch (Exception e){
            log.info("SubjectLabelController.update.error:{}",e.getMessage(),e);
            return Result.FAILED(e.getMessage());
        }

    }

    /**
     * 查询分类下的标签
     * @param subjectLabelDTO
     * @return
     */

    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.queryLabelByCategoryId.dto:{}",
                        JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类id不能为空");
            SubjectLabelBO subjectLabelBO  = SubjectLabelDTOConverter.INSTANCE.convertDtoToBo(subjectLabelDTO);
            List<SubjectLabelBO> resultList = subjectDomainLabelService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOS = SubjectLabelDTOConverter.INSTANCE.convertBoToDTOList(resultList);
            return Result.OK(subjectLabelDTOS);
        }catch (Exception e){
            log.info("SubjectLabelController.queryLabelByCategoryId.error:{}",e.getMessage(),e);
            return Result.FAILED(e.getMessage());
        }

    }

}
